﻿
StrToNeedle(str)
{
    Needle := "i)"
		Loop, Parse, str,
		{
			 char := RegExReplace(A_LoopField,"\+|\?|\.|\*|\{|\}|\(|\)|\||\^|\$|\[|\]|\\","\$0")
			 char := RegExReplace(char,"\s","\s")
			 Needle .= "(" char "|\[[^\[]*" char "[^\[]*\])"
		}
    return Needle
}

; 字符串转换为首字母，完整转换
StrToPY(str) ; IsObj 控制返回的是对像还是字符串
{
    rStr := ""
    if not RegExMatch(str,"[^x00-xff]") ;无汉字直接返回
    {
        return str
    }
    Loop, Parse, str
    {
        char := PY_Char(A_loopfield)
        If strlen(char) or InStr("[]", A_LoopField) ; 多音字 或者"["、"]"
        {
            rStr .= "[" A_Loopfield char "]" ; 多音字用"[]"保存起来，便于正则使用
        }
        else
        {
            rStr .= A_Loopfield
        }
    }
    return rStr
}

; 单字符转换为首字母
PY_Char(char)
{
    global PinyinTable
    return PinyinTable[Asc(char)]
}

; 建立拼音码表
PY_BuildTable(PinyinFile)
{
    global PinyinTable := Object()
    if Not FileExist(PinyinFile)
    {
        msgbox , %file% Not Exist !
        return
    }
    fileRead, table, %PinyinFile%
		Loop, Parse, table, `n, `r
		{
				code := RegExReplace(A_loopfield, "=.*$")
				py := RegExReplace(A_loopfield, "^.*=")
        PinyinTable[code] := py
    }
}
